package com.chuangke.file.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import com.chuangke.common.exception.ChuangkeException;

public class DateUtil {
	public static long getLastWeekTime() {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.DATE, - 7);
        Date d = c.getTime();
		return d.getTime();
	}
	public static long getLastMonthTime() {
		Calendar c = Calendar.getInstance();
		c.setTime(new Date());
        c.add(Calendar.MONTH, -1);
        Date m = c.getTime();
		return m.getTime();
	}
	public static String getLastMonthDate() {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar c = Calendar.getInstance();
		c.setTime(new Date());
        c.add(Calendar.MONTH, -1);
        Date m = c.getTime();
        return format.format(m);
	}
	public static long getLastYearTime() {
		Calendar c = Calendar.getInstance();
		c.setTime(new Date());
        c.add(Calendar.YEAR, -1);
        Date y = c.getTime();
		return y.getTime();
	}
	//获取日期年
	public static int getTheYear(String dateStr) {
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");  
		Date date = null;  
		try {  
		    date = format.parse(dateStr);  
		} catch (ParseException e) {  
		    e.printStackTrace();  
		}  
		Calendar calendar = Calendar.getInstance();  
		calendar.setTime(date);  
		return calendar.get(Calendar.YEAR);  
	}
	//获取日期月
	public static int getTheMonth(String dateStr) {
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");  
		Date date = null;  
		try {  
		    date = format.parse(dateStr);  
		} catch (ParseException e) {  
		    e.printStackTrace();  
		}  
		Calendar calendar = Calendar.getInstance();  
		calendar.setTime(date);  
		return calendar.get(Calendar.MONTH)+1;  
	}
	//获取日期周数
	public static int getTheWeek(String dateStr) {
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");  
		Date date = null;  
		try {  
		    date = format.parse(dateStr);  
		} catch (ParseException e) {  
		    e.printStackTrace();  
		}  
		Calendar calendar = Calendar.getInstance();  
		calendar.setFirstDayOfWeek(Calendar.MONDAY);  
		calendar.setTime(date);  
		return calendar.get(Calendar.WEEK_OF_YEAR);  
	}
	//获取日期是一周的第几天
	public static int getTheDayOfWeek(String dateStr) {
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");  
		Date date = null;  
		try {  
		    date = format.parse(dateStr);  
		} catch (ParseException e) {  
		    e.printStackTrace();  
		}  
		Calendar calendar = Calendar.getInstance();  
		calendar.setTime(date);  
		return calendar.get(Calendar.DAY_OF_WEEK)-1;  
	}
	//获取日期是一月的第几天
	public static int getTheDayOfMonth(String dateStr) {
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");  
		Date date = null;  
		try {  
		    date = format.parse(dateStr);  
		} catch (ParseException e) {  
		    e.printStackTrace();  
		}  
		Calendar calendar = Calendar.getInstance();  
		calendar.setTime(date);  
		return calendar.get(Calendar.DAY_OF_MONTH);  
	}
	//将字符串日期转换成毫秒
	public static long strDateTomills(String dateStr) {
		  Calendar calendar = Calendar.getInstance();
		  try {
			calendar.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dateStr));
		  } catch (ParseException e) {
			throw new ChuangkeException(e.getMessage());
		  }
		  return calendar.getTimeInMillis();
	}
}
